package tmproject.hlhj.fhp.cartrans.activitys

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Paint
import android.net.Uri
import android.os.Build
import android.webkit.WebView
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.aty_notce_detail.*
import tmproject.hlhj.fhp.cartrans.BaseAty
import tmproject.hlhj.fhp.cartrans.IView.NoticeDetailView
import tmproject.hlhj.fhp.cartrans.R
import tmproject.hlhj.fhp.cartrans.javaBean.NoticeDetailBean
import tmproject.hlhj.fhp.cartrans.presenter.NoticeDetailPresenter
import tmproject.hlhj.fhp.cartrans.utils.SpHelper

/**
 * Created by Never Fear   on 2018\8\28 0028.
Never More....
 */
class NoticeDetailAty :BaseAty<NoticeDetailView,NoticeDetailPresenter>(), NoticeDetailView {
    override fun bindView(): NoticeDetailView {
        return this
    }

    override fun createPresenter(): NoticeDetailPresenter {
        return NoticeDetailPresenter()
    }

    override fun getContentId(): Int {
        return R.layout.aty_notce_detail
    }

    override fun initView() {
        initWeb()
    }
    @SuppressLint("WrongConstant")
    private fun initWeb() {
        val webSettings = webView.getSettings()
        if (Build.VERSION.SDK_INT >= 19) {
            webSettings.setCacheMode(1)
        }

        if (Build.VERSION.SDK_INT >= 19) {
            webSettings.setLoadsImagesAutomatically(true)
        } else {
            webSettings.setLoadsImagesAutomatically(false)
        }

        if (Build.VERSION.SDK_INT >= 11) {
            webView.setLayerType(1, null as Paint?)
        }

        webView.setLayerType(2, null as Paint?)
        webSettings.setJavaScriptEnabled(true)
        webSettings.setSupportZoom(true)
        webSettings.setBuiltInZoomControls(true)
        webSettings.setDisplayZoomControls(false)
//        webSettings.setUseWideViewPort(true)
//        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN)
        webSettings.setLoadWithOverviewMode(true)
        webSettings.setDatabaseEnabled(true)
        webSettings.setSavePassword(true)
        webSettings.setDomStorageEnabled(true)
        webView.setSaveEnabled(true)
        webView.setKeepScreenOn(true)
        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                if (!url.startsWith("http:") && !url.startsWith("https:")) {
                    try {
                        val intent = Intent("android.intent.action.VIEW", Uri.parse(url))
                        startActivity(intent)
                        return true
                    } catch (e: Exception) {

                    } finally {
                        return true
                    }
                } else {
                    view.loadUrl(url)
                    return false
                }
            }
        }
    }
    override fun logicStart() {
        presenter?.getNoticeDetail(this,SpHelper.getToken(this),intent.getIntExtra("id",-1))
    }
    override fun setDetailInfo(noticeDetailBean: NoticeDetailBean) {
        tvTittle.text=noticeDetailBean.data.title
        val htmlStr = noticeDetailBean.data.content.replace("<img", "<img width=100%")
        webView.loadDataWithBaseURL(null,htmlStr,"text/html", "utf-8",null)
    }
    override fun setListener() {
        btExit.setOnClickListener {
            finish()
        }
    }
}